﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SerialChart
{
    public class Parser : DataParser
    {
        public bool IsDataUpdate = false;

        public List<int> DataList = new List<int>();
        public RichTextBox txtData;
        public bool IsHex = true;

        private byte[] recData = new byte[10];
        private int recPtr = 0;
        enum RecState { IDLE, HEADREADY, RECING, };
        RecState recState = RecState.IDLE;

        public void getFrame(byte[] dataBuff)
        {
            if (IsHex)
            {
                foreach (byte data in dataBuff)
                {
                    txtData.AppendText(data.ToString("X2") + " ");
                }
            }
            else
            {
                txtData.AppendText(Encoding.Default.GetString(dataBuff));
            }
            foreach (byte data in dataBuff)
            {
                switch (recState)
                {
                    case RecState.IDLE:
                        if (data == 0xA5)
                        {
                            recPtr = 0;
                            recData[recPtr++] = 0xA5;
                            recState = RecState.HEADREADY;
                        }
                        break;
                    case RecState.HEADREADY:
                        if (data == 0x5A)
                        {
                            recData[recPtr++] = 0x5A;
                            recState = RecState.RECING;
                        }
                        else
                        {
                            recPtr = 0;
                            recState = RecState.IDLE;
                        }
                        break;
                    case RecState.RECING:
                        if (recPtr < 10)
                            recData[recPtr++] = data;
                        else
                        {
                            //OnFrameReceived(new FrameRecEventArgs(recData));
                            process(recData);
                            recPtr = 0;
                            recState = RecState.IDLE;
                        }
                        break;
                }

                IsDataUpdate = true;
            }
        }

        private void process(Byte[] recData)
        {
            if (recData[2] == 0x00)//验证地址
            {
                switch (recData[3])//执行指令
                {
                    case 0x00:
                        if (recData[4] == 0x00)//int型数据
                        {
                            List_AddData(BitConverter.ToInt32(recData, 5));
                        }
                        break;
                    case 0x01:
                        break;
                    case 0x02:
                        break;
                    case 0x03:
                        break;
                }
            }

        }

        public void List_AddData(int Data)
        {
            // this.chart1.Series[0].Points.Add(Data);
            DataList.Add(Data);
            if (DataList.Count > 100)
                DataList.RemoveAt(0);
        }

        public void GetData(byte[] data)
        {
            getFrame(data);
        }
    }
}
